package test.testThread;

import java.util.HashMap;
import java.util.Map;

public class UserMap {
	private static UserMap userMap = new UserMap();
	public static UserMap getInstance(){
		return userMap;
	}
	
	public static final String STATEREPORT="stateRepoart";
	public static final String GETRECEIVEMSG="getReceiveMsg";
	
	private Map<String, String> stateReportMap = new HashMap<String, String>();
	private Map<String, String> getReceiveMsgMap = new HashMap<String, String>();
	
	public void addUserMap(String key, String value,String flag,Object servletObject) {
		synchronized (servletObject) {
			Map<String, String> tempMap=getMap(flag,servletObject);
			if (!tempMap.containsKey(key)) {
				tempMap.put(key, value);
			}
		}
	}
	
	public Map<String, String> getMap(String flag,Object servletObject) {//这里线程是不安全的
		if(flag.equals(STATEREPORT)){
			//System.out.println(Thread.currentThread().getName()+"*****111111Start*******");
			//print("aaaaaa", servletObject);
			//System.out.println(Thread.currentThread().getName()+"*****111111end*******");
			return stateReportMap;
		}else if(flag.equals(GETRECEIVEMSG)){
			//System.out.println(Thread.currentThread().getName()+"*****222222Start*******");
			//print("bbbbbb", servletObject);
			//System.out.println(Thread.currentThread().getName()+"*****222222end*******");
			return getReceiveMsgMap;
		}
		return null;
	}
	
	public void print(String name,Object servletObject){
		synchronized(servletObject){
			for(int i=0;i<name.length();i++){
				System.out.print(name.charAt(i));
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println();
		}
	}

	public Map<String, String> getStateReportMap() {
		return stateReportMap;
	}

	public Map<String, String> getGetReceiveMsgMap() {
		return getReceiveMsgMap;
	}
	
}
